package com.yuyue.springFramework.note14;

import com.yuyue.springFramework.note14.dao.PersonDao;
import com.yuyue.springFramework.note14.entity.Animal;
import com.yuyue.springFramework.note14.entity.Human;
import com.yuyue.springFramework.note14.entity.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 《Spring-14 面向切面编程》文章代码
 *  文章地址：https://mp.weixin.qq.com/s/Db4BuFZj1giy3heYd-ZD2A
 */
public class Note14Test {
    @Test
    // 初识切面编程。前增强
    public void fun1(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MyConfig.class);
        applicationContext.refresh();

        Person person = applicationContext.getBean("person", Person.class);
        PersonDao personDao = applicationContext.getBean("personDao", PersonDao.class);

        //赋值
        person.setName("胡歌");
        person.setAge(18);
        person.setSex("男");

        //模拟插表
        personDao.insertTable(person);
    }

    @Test
    // 成功返回增强
    public void fun2(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MyConfig.class);
        applicationContext.refresh();

        Person person = applicationContext.getBean("person", Person.class);
        PersonDao personDao = applicationContext.getBean("personDao", PersonDao.class);

        //赋值
        person.setName("胡歌");
        person.setAge(18);
        person.setSex("男");

        //模拟更新表
        personDao.updateTable(person);

    }

    @Test
    // 声明一个介绍
    public void fun3(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MyConfig.class);
        applicationContext.refresh();

        //接口接收实现类
        //Woman woman = applicationContext.getBean("woman", Woman.class);//报错，必须使用接口获取
        Human woman = (Human) applicationContext.getBean("woman");
        woman.speak();

        System.out.println("---- 生气之后 ----");
        //强转为“父”接口的实现类
        Animal lion = (Animal)woman;
        lion.roar();
    }

    @Test
    // xml风格前增强
    public void fun4(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext06.xml");

        Person person = applicationContext.getBean("person", Person.class);
        PersonDao personDao = applicationContext.getBean("personDao", PersonDao.class);

        //赋值
        person.setName("冯宝宝");
        person.setAge(18);
        person.setSex("女");

        //模拟插表
        personDao.insertTable(person);
    }

    @Test
    // xml其他增强
    public void fun5(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext06.xml");

        Person person = applicationContext.getBean("person", Person.class);
        PersonDao personDao = applicationContext.getBean("personDao", PersonDao.class);

        //赋值
        person.setName("冯宝宝");
        person.setAge(18);
        person.setSex("女");

        //模拟更新表
        personDao.updateTable(person);
    }

    @Test
    // xml风格介绍
    public void fun6(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext06.xml");

        //Woman woman = applicationContext.getBean("woman", Woman.class);//报错，必须使用接口获取
        Human woman = applicationContext.getBean("woman", Human.class);//报错，必须使用接口获取
        woman.speak();
        System.out.println("----生气之后----");
        ((Animal)woman).roar();
    }
}
